//
//  NSFileManager+ZOD.m
//  ZTEOnlineDoc
//
//  Created by 万伟琦 on 2018/9/10.
//  Copyright © 2018年 万伟琦. All rights reserved.
//

#import "NSFileManager+ZOD.h"
#import "ZODGlobalStorage.h"

@implementation NSFileManager (ZOD)


- (NSURL *)getSDKRootCacheDirectoryURL {
    NSURL *cacheURL = [[self URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
    //    NSURL *cacheURL = [[self URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *udmCacheURL = [cacheURL URLByAppendingPathComponent:@"cn.com.zte.onlineRead.sdk" isDirectory:YES];
    NSString *udmCachePath = udmCacheURL.path;
    
    BOOL isDirExisted = [self fileExistsAtPath:udmCachePath isDirectory:NULL];
    if (!isDirExisted) {
        [self createDirectoryAtURL:udmCacheURL withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return udmCacheURL;
}

- (NSURL *)getCacheDirectoryURLByCurrentAccount {
    NSURL *cacheURL = [self getSDKRootCacheDirectoryURL];
    NSString *uno = [ZODGlobalStorage sharedStorage].configuration.userNo;//onLineRead
    NSURL *accountCacheURL = [cacheURL URLByAppendingPathComponent:uno isDirectory:YES];
    NSString *accountCachePath = accountCacheURL.path;
    
    BOOL isDirExisted = [self fileExistsAtPath:accountCachePath isDirectory:NULL];
    if (!isDirExisted) {
        [self createDirectoryAtURL:accountCacheURL withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return accountCacheURL;
}

- (NSURL *)getDocumentDirectoryURLByDocMD5:(NSString *)docMD5 {
    NSURL *cacheURL = [self getCacheDirectoryURLByCurrentAccount];
    NSURL *docDirURL = [cacheURL URLByAppendingPathComponent:docMD5];
    
    BOOL isDirExisted = [self fileExistsAtPath:docDirURL.path];
    if (!isDirExisted) {
        [self createDirectoryAtURL:docDirURL withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    return docDirURL;
}

- (NSURL *)getDocCacheIndexFileURLByDocMD5:(NSString *)docMD5 {
    NSURL *docCacheURL = [self getDocumentDirectoryURLByDocMD5:docMD5];
    NSURL *indexFileURL = [docCacheURL URLByAppendingPathComponent:@"index"];
    
    BOOL isIndexExisted = [self fileExistsAtPath:indexFileURL.path];
    if (!isIndexExisted) {
        [self createFileAtPath:indexFileURL.path contents:nil attributes:nil];
    }
    return indexFileURL;
}

//计算文件夹下文件的总大小
- (NSUInteger)getFileSizeAtPath:(NSString*)path
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSUInteger size = 0;
    NSArray* subFiles = [fileManager contentsOfDirectoryAtPath:path error:nil];
    for(int i = 0; i < subFiles.count; i++)
    {
        NSString *fullPath = [path stringByAppendingPathComponent:[subFiles objectAtIndex:i]];
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
        {
            NSDictionary *fileAttributeDic = [fileManager attributesOfItemAtPath:fullPath error:nil];
            size += fileAttributeDic.fileSize / 1024.0;
        }
        else
        {
            [self getFileSizeAtPath:fullPath];
        }
    }
    return size;
}

@end
